//
//  AppDelegate.m
//  WHISPER
//
//  Created by lanou3g on 15/12/16.
//  Copyright © 2015年 王艳苹. All rights reserved.
//

#import "AppDelegate.h"
#import "MZRootViewController.h"
#import "UIView+Extention.h"
#import <AVFoundation/AVFoundation.h>
#import <AVOSCloud/AVOSCloud.h>

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//新浪微博SDK头文件
#import "WeiboSDK.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[MZRootViewController alloc] init];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [AVOSCloud setApplicationId:@"e79hNIMy1QSYy7k0pCf6lVNc-gzGzoHsz"
                      clientKey:@"W6GzvtvAD9nKWynjap7GaLRK"];
    [ShareSDK registerApp:@"df89dc48a853"
     
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeTencentWeibo)
                            ]
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             case SSDKPlatformTypeTencentWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo:
                 //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"//3880708977
                                           appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"//38a4f8204cc784f81f9f0daaf31e02e3 0dd958de1bf819814ec50bfe594403bf
                                         redirectUri:@"http://www.sharesdk.cn"
                                            authType:SSDKAuthTypeBoth];
                 break;
                 case SSDKPlatformTypeTencentWeibo:
                 [appInfo SSDKSetupTencentWeiboByAppKey:@"801307650" appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c" redirectUri:@"http://www.sharesdk.cn"];
                 break;
            default:
                 break;
         }
     }];
    //获得后台播放的权利
    NSError* error;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
    
    NSInteger index = arc4random() % 3 + 1;
    NSString *imageName = [NSString stringWithFormat:@"intro_0%ld.jpg",index];
    UIImageView *niceView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    niceView.image = [UIImage imageNamed:imageName];
    [self.window addSubview:niceView];
    [self.window bringSubviewToFront:niceView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:3.0];
    [UIView setAnimationTransition:(UIViewAnimationTransitionNone) forView:self.window cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    niceView.tag = 119;
    niceView.transform = CGAffineTransformMakeScale(1.2, 1.2);
    [UIView commitAnimations];
    return YES;
}
- (void)startupAnimationDone:(UIViewAnimationTransition)animation finished:(NSError *)error context:(UIImageView *)niceView{
    niceView = [self.window viewWithTag:119];
    [UIView animateWithDuration:0.3 animations:^{
        niceView.width = 0;
    }];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    //让应用在后台运行
    [application beginBackgroundTaskWithExpirationHandler:nil];

}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
